package yznu_Servlet_menu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jboss.weld.context.RequestContext;

import Goods_Utils.Good;
import Order_Utils.Order;
import User_Utils.User;
import index_Model.Join;
import yznu_Bean_handle.goodsManager;
import yznu_Bean_handle.joinManager;
import yznu_Bean_handle.Delete;
import yznu_Bean_handle.Query;
import yznu_Bean_handle.othersStoreOrder;
import yznu_Bean_handle.vipManager;
import yznu_Servlet_good.*;
import yznu_Bean_handle.mainStoreOrder;
import yznu_Bean_handle.otherStoreManager;
import yznu_utils.Store;
public class MenuSer extends HttpServlet {

	String status = "";
	String Msg = "";
	String type = "";
	String Username = "";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession(true);
		response.setContentType("text/html;charset=utf-8");
		status = request.getParameter("status");
		type = (String) session.getAttribute("type");
		Username = (String) session.getAttribute("User");
		
		if(status.equals("homePage")){
			RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/main.jsp");
			dispatcher.forward(request, response);
			
		}else if(status.equals("mainStoreOrder")){
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			request.getSession().setAttribute("currentPage", currentPage);
			int countPage=new mainStoreOrder().countPage();
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<Order> orders = new mainStoreOrder().selectAll(currentPage);
			request.setAttribute("orders", orders);
			RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/mainStoreOrder.jsp");
			dispatcher.forward(request, response);
			
		}else if(status.equals("othersStoreOrder")){
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			request.getSession().setAttribute("currentPage", currentPage);
			int countPage=new othersStoreOrder().countPage();
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<Order> orders = new othersStoreOrder().selectAll(currentPage);
			request.setAttribute("orders", orders);
			RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/othersStoreOrder.jsp");
			dispatcher.forward(request, response);
			
		}else if(status.equals("goods")){
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			request.getSession().setAttribute("currentPage", currentPage);
			int countPage=new goodsManager().countPage();
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<Good> goods = new goodsManager().selectAll(currentPage);

			request.setAttribute("goods", goods);
			RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/goods.jsp");
			dispatcher.forward(request, response);
			
		}else if(status.equals("vip")){
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			request.getSession().setAttribute("currentPage", currentPage);
			int countPage=new vipManager().countPage();
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<User> users = new vipManager().selectAll(currentPage);

			request.setAttribute("users", users);
			RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/users.jsp");
			dispatcher.forward(request, response);
		}else if(status.equals("store")){
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			request.getSession().setAttribute("currentPage", currentPage);
			int countPage=new otherStoreManager().countPage();
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<Store> stores = new otherStoreManager().selectAll(currentPage);

			request.setAttribute("stores", stores);
			RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/stores.jsp");
			dispatcher.forward(request, response);
		}else if(status.equals("join")){
			
			if("del".equals(request.getParameter("m"))){
				new Delete().Del_join(new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8"));
			}
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			request.getSession().setAttribute("currentPage", currentPage);
			int countPage=new otherStoreManager().countPage();
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<Join> joins = new joinManager().selectAll(currentPage);

			request.setAttribute("joins", joins);
			RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/joins.jsp");
			dispatcher.forward(request, response);
		}else if(status.equals("Quit")){
			session.removeAttribute("User");
			Msg = "退出成功";
			request.setAttribute("Msg",Msg);
			RequestDispatcher dispatcher=request.getRequestDispatcher("index.jsp");
			dispatcher.forward(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
